<?php

namespace app\controller\admin\genshinteam;

use app\model\api\genshin\GenshinMaterials;
use app\model\api\GenshinSkillLevel;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use laytp\library\Tree;

/**
 * 菜单控制器
 */
class SkillLevel extends Backend
{
    public $model;
    protected $materials_model;
    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public function _initialize()
    {
        $this->model = new GenshinSkillLevel();
        $this->materials_model = new GenshinMaterials();
    }

    public function index()
    {
        $where   = $this->buildSearchParams();
        $order   = $this->buildOrder();
        $allData = $this->request->param('all_data');
        if ($allData) {
            $data = $this->model->hasWhere('role',function ($query){
                return $query->whereNotNull('id');
            })->with('role')->where($where)->order($order)->cache(true)->select()->toArray();
            $data = array_map(function ($item){
                $item['role_name'] = $item['role']['role_name'];
                $item['skill_name'] = json_decode($item['role']['skill'],true)[$item['skill_id']-1]['name'];
                unset($item['role']);
                return $item;
            },$data);
        } else {
            $limit = $this->request->param('limit', 10);
            $data  = $this->model->hasWhere('role',function($query){
                $query->whereNotNull('id');
            })->with('role')->where($where)->order($order)->paginate($limit)->toArray();
            $data['data'] = array_map(function ($item){
                $item['role_name'] = $item['role']['role_name'];
                $item['skill_name'] = json_decode($item['role']['skill'],true)[$item['skill_id']-1]['name'];
                unset($item['role']);
                return $item;
            },$data['data']);
        }
        return $this->success('数据获取成功', $data);
    }


    public function skill($is_json = false)
    {
        $data = [
            ['id' => 1,'name'=>'一技能'],
            ['id' => 2,'name'=>'二技能'],
            ['id' => 3,'name'=>'三技能'],
        ];
        if ($is_json) {
            return $data;
        }
        return $this->success('',$data);
    }
    protected function getNormal(){
        $list = $this->materials_model->where('type','skillNormal')->select()->toArray();
        return array_column($list,"materials_id");
    }

    //查看详情
    public function info()
    {
        $id   = $this->request->param('id');
        $info = $this->model->find($id);
        $info->material = json_decode($info->material,true);
        $ids = $this->getNormal();
        $materials = [];
        foreach ($info->material['avatar_skill_consume'] as $item){
            if(!in_array($item['id'],$ids)){
                $materials[] =[
                    'id'  =>$item['id'],
                    'number'  =>$item['num'],
                ];
            }
        }
        $info->material = $materials;

        return $this->success('获取成功', $info);
    }



    /**
     * 编辑材料
     * @return false|string|\think\response\Json
     */
    public function edit(){
        $post     = input("param.");
        $detail = $this->model->find($post['id']);
        empty($detail) && $this->error('数据不存在');
        $detail = $detail->toArray();
        $common_material = $this->getNormal();
        $materials = json_decode($detail['material'],true);
        $new_material = [];
        foreach ($materials['avatar_skill_consume'] as $item){
            if(in_array($item['id'],$common_material)){
                $new_material[$item['id']] = $item;
            }
        }
        $ids = array_column($post['material'],'id');
        $post['material'] = array_combine($ids,$post['material']);
        $list = $this->materials_model->whereIn('materials_id',$ids)->field('id,materials_id,icon,name')->select()->toArray();
        foreach ($list as $item){
            $new_material[$item['materials_id']] = [
                'id'=>$item['materials_id'] ,
                'num'=> $post['material'][$item['materials_id']]['number'],
                'name'=> $item['name'],
                'icon'=> $item['icon'],
            ];
        }
        $materials['avatar_skill_consume'] = array_values($new_material);
        try {
            $save  = $this->model->where('id',$post['id'])->save(
                [
                    'material' => json_encode($materials,256),
                ]
            );
            if (!$save) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }

    }
}
